#!/usr/bin/env python3
"""
光标监控库使用示例
"""

import time
import signal
import sys
from cursor_monitor import CursorMonitor, get_cursor_id, get_cursor_position, get_cursor_status

# 全局标志位
running = True

def signal_handler(sig, frame):
    global running
    print("\n收到停止信号，退出...")
    running = False

def example_simple_functions():
    """示例1: 使用简化函数"""
    print("=== 简化函数示例 ===")
    
    # 获取光标ID
    cursor_id = get_cursor_id()
    print(f"光标ID: {cursor_id}")
    
    # 获取光标位置
    position = get_cursor_position()
    if position:
        print(f"光标位置: {position}")
    
    # 获取完整状态
    status = get_cursor_status()
    if status:
        print(f"完整状态: 位置=({status.x}, {status.y}), ID={status.cursor_id}, "
              f"尺寸={status.width}x{status.height}, 可见={bool(status.is_visible)}")
    
    print()

def example_context_manager():
    """示例2: 使用上下文管理器"""
    print("=== 上下文管理器示例 ===")
    
    with CursorMonitor() as monitor:
        print(f"库版本: {monitor.get_version()}")
        print(f"XFixes扩展可用: {monitor.is_xfixes_available()}")
        
        status = monitor.get_status()
        if status:
            print(f"光标状态: 位置=({status.x}, {status.y}), ID={status.cursor_id}")
    
    print()

def example_realtime_monitoring():
    """示例3: 实时监控"""
    print("=== 实时监控示例 ===")
    print("按 Ctrl+C 停止监控")
    
    # 注册信号处理
    signal.signal(signal.SIGINT, signal_handler)
    
    with CursorMonitor() as monitor:
        print(f"开始实时监控，库版本: {monitor.get_version()}")
        print("移动鼠标观察光标状态变化...")
        print()
        
        prev_status = None
        update_count = 0
        
        while running and update_count < 100:  # 最多监控100次更新
            status = monitor.get_status()
            if status:
                # 只在状态变化时显示
                if prev_status is None or (
                    status.x != prev_status.x or 
                    status.y != prev_status.y or 
                    status.cursor_id != prev_status.cursor_id
                ):
                    print(f"位置: ({status.x:4d}, {status.y:4d}) | "
                          f"ID: {status.cursor_id:10d} | "
                          f"尺寸: {status.width:3d}x{status.height:3d} | "
                          f"可见: {'是' if status.is_visible else '否'}")
                    prev_status = status
                    update_count += 1
            
            time.sleep(0.1)  # 100ms间隔

def example_custom_library_path():
    """示例4: 使用自定义库路径"""
    print("=== 自定义库路径示例 ===")
    
    # 指定库路径
    lib_path = "./libcursormonitor.so"
    
    try:
        with CursorMonitor(lib_path=lib_path) as monitor:
            print(f"使用自定义路径加载库: {lib_path}")
            print(f"版本: {monitor.get_version()}")
            
            cursor_id = monitor.get_cursor_id()
            print(f"当前光标ID: {cursor_id}")
    except Exception as e:
        print(f"错误: {e}")
    
    print()

if __name__ == "__main__":
    print("光标监控库Python绑定示例")
    print("=" * 50)
    
    # 运行所有示例
    example_simple_functions()
    example_context_manager()
    example_custom_library_path()
    example_realtime_monitoring()
    
    print("示例程序结束")